const app = getApp()
var xrRequest = require('../../../vendor/hope-sdk/core/request.js');
var config = require('../../../config.js');
var xrSdk = require('../../../vendor/hope-sdk/index.js');
const util = require('../../../utils/util.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    coupon:null,
    car:null,
    shop:null,
    service:null,
    latitude  :0,
    longitude :0,
    description:'',
    allow_submit:false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var data =  JSON.parse(options.data);
    console.log(data);
    this.init(data);
  },
  init:function(data){
    this.setData({
      car      :data.car,
      shop     :data.shop,
      service  :data.service,
      latitude :data.latitude,
      longitude:data.longitude
    });
    this.prepare_rescue();
  },
  prepare_rescue:function(){
    var data = {
      longitude: this.data.longitude,
      latitude: this.data.latitude,
      shop_id: this.data.shop.id,
      car_id: this.data.car.id,
      service_id: this.data.service.id
    };
    var that = this;
    wx.showLoading({
      title: '请稍候...',
      mask: true
    })
    xrRequest.request(app,{
      url: config.service.prepare_rescue,
      login: true,
      data: data,
      success: function (res) {
        that.setData({
          coupon : res.data.data.coupon
        })
        wx.hideLoading()
      },
      fail: function (res) {
        wx.hideLoading()
        wx.showModal({
            title: '提示',
            content: res,
            showCancel: false,//是否显示取消按钮
            confirmText:"确定",//默认是“确定”
            success: function (res) {
              wx.navigateBack({
                complete: (res) => {},
              })
            },
            fail: function (res) { },//接口调用失败的回调函数
            complete: function (res) { },//接口调用结束的回调函数（调用成功、失败都会执行）
        })
       },
      authfail: function (e) {
        wx.hideLoading()
        wx.navigateTo({
          url: "../../auth/auth"
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  sureClick:function(e){
    var that = this;
    wx.showModal({
      title: '确定提交吗',
      content: '确定提交道路救援申请吗？',
      success(res) {
        if (res.confirm) {
          that.sureHandler();
          console.log('用户点确定了')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    });    
  },
  sureHandler:function(order_id){
    if(this.data.longitude <=0){
      util.toast('经度不能为空');
      return
    }
    if(this.data.latitude <=0){
      util.toast('纬度不能为空');
      return
    }
    if(this.data.shop ==null){
      util.toast('救援门店不能为空');
      return
    }
    if(this.data.car ==null){
      util.toast('车辆不能为空');
      return
    }
    if(this.data.service ==null){
      util.toast('救援服务不能为空');
      return
    }
    var data = {
      longitude  : this.data.longitude,
      latitude   : this.data.latitude,
      shop_id    : this.data.shop.id,
      car_id     : this.data.car.id,
      service_id : this.data.service.id,
      remark     : this.data.description
    };
    if(this.data.coupon != null && this.data.coupon != undefined){
      data.coupon_id = this.data.coupon.id;
    }

    var that = this;
    xrRequest.request(app,{
      url: config.service.apply_rescue,
      login: true,
      data: data,
      success: function (res) {
        if (res.data.code == 1) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          });          

          var pages = getCurrentPages();
          var prevPage = pages[pages.length - 2]; //上一个页面
          prevPage.setData({
            'showSuccess': true
          }, function () {
            wx.navigateBack({
              delta: 1
            });
          });


        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: res,
          icon: 'none',
        })
       },
      authfail: function (e) {
        wx.navigateTo({
          url: "../../auth/auth"
        })
      }
    })
  },
  descriptionInput:function(e){
    this.setData({
      description: e.detail.value
    });
  },
  reselect:function(){
    wx.navigateBack({
      complete: (res) => {},
    })
  },
  selectCoupon:function(e){
    var car_id = this.data.car.id;
    wx.navigateTo({
      url: '../coupon/select?id=' + car_id,
    })
  }
})